package svg.hw.recog;


public class DownSampler {

    /**
     * The downsample width for the application.
     */
    protected int downSampleWidth;

    /**
     * The down sample height for the application.
     */
    protected int downSampleHeight;


    /**
     * The constructor.
     */
    public DownSampler(int downSampleWidth, int downSampleHeight) {
        this.downSampleWidth = downSampleWidth;
        this.downSampleHeight = downSampleHeight;
    }


    /**
     * Called to downsample the image and store
     * it in the image data component.
     */
    public ImageData downSample(ImageData drawnPixels) {

        // now downsample
        ImageData data =
            new ImageData(downSampleWidth, downSampleHeight);

        int w = drawnPixels.getWidth();
        int h = drawnPixels.getHeight();

        for (int y1 = 0; y1 < drawnPixels.getHeight(); y1++) {
            for (int x1 = 0; x1 < drawnPixels.getWidth(); x1++) {
                if (drawnPixels.getData(x1, y1) != 0) {
                    int x2 = (downSampleWidth * x1) / w;
                    int y2 = (downSampleHeight * y1) / h;
                    data.setData(x2, y2, 1);
                }
            }
        }

        //pixelMap = data;
        //repaint();

        return data;
    }
}
